package com.ruoyi.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.edu.domain.vo.EduChapterVo;
import com.ruoyi.edu.domain.vo.EduCourseVo;
import com.ruoyi.edu.mapper.EduChapterMapper;
import com.ruoyi.edu.mapper.EduCourseMapper;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.mapper.SysOssMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.edu.domain.bo.EduVideoBo;
import com.ruoyi.edu.domain.vo.EduVideoVo;
import com.ruoyi.edu.domain.EduVideo;
import com.ruoyi.edu.mapper.EduVideoMapper;
import com.ruoyi.edu.service.IEduVideoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 课程视频Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-18
 */
@RequiredArgsConstructor
@Service
public class EduVideoServiceImpl implements IEduVideoService {

    private final EduVideoMapper baseMapper;
    private final SysOssMapper sysOssMapper;
    private final EduChapterMapper eduChapterMapper;
    private final EduCourseMapper eduCourseMapper;

    /**
     * 查询课程视频
     */
    @Override
    public EduVideoVo queryById(String id){
        EduVideoVo eduVideoVo = baseMapper.selectVoById(id);
        EduChapterVo eduChapterVo = eduChapterMapper.selectVoById(eduVideoVo.getChapterId());
        eduVideoVo.setTitleC(eduChapterVo.getTitle());
//        EduCourseVo eduCourseVo = eduCourseMapper.selectVoById(eduChapterVo.getCourseId());
//        eduChapterVo.setTitleC(eduCourseVo.getTitle());
        EduCourseVo eduCourseVo = eduCourseMapper.selectVoById(eduVideoVo.getCourseId());
        eduVideoVo.setTitleCo(eduCourseVo.getTitle());
        return eduVideoVo;
    }

    /**
     * 查询课程视频列表
     */
    @Override
    public TableDataInfo<EduVideoVo> queryPageList(EduVideoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EduVideo> lqw = buildQueryWrapper(bo);
        Page<EduVideoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<EduVideoVo> records = result.getRecords();
        ArrayList<EduVideoVo> list = new ArrayList<>();
        for (EduVideoVo eduVideoVo :records){
            SysOssVo sysOssVo = sysOssMapper.selectVoById(eduVideoVo.getVideoSourceId());
            eduVideoVo.setUrl(sysOssVo.getUrl());
            EduChapterVo eduChapterVo = eduChapterMapper.selectVoById(eduVideoVo.getChapterId());
            eduVideoVo.setTitleC(eduChapterVo.getTitle());
            EduCourseVo eduCourseVo = eduCourseMapper.selectVoById(eduVideoVo.getCourseId());
            eduVideoVo.setTitleCo(eduCourseVo.getTitle());
            list.add(eduVideoVo);
        }
        return TableDataInfo.build(list,result.getTotal());
    }

    /**
     * 查询课程视频列表
     */
    @Override
    public List<EduVideoVo> queryList(EduVideoBo bo) {
        LambdaQueryWrapper<EduVideo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EduVideo> buildQueryWrapper(EduVideoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EduVideo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCourseId()), EduVideo::getCourseId, bo.getCourseId());
        lqw.eq(StringUtils.isNotBlank(bo.getChapterId()), EduVideo::getChapterId, bo.getChapterId());
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), EduVideo::getTitle, bo.getTitle());
        return lqw;
    }

    /**
     * 新增课程视频
     */
    @Override
    public Boolean insertByBo(EduVideoBo bo) {
        EduVideo add = BeanUtil.toBean(bo, EduVideo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改课程视频
     */
    @Override
    public Boolean updateByBo(EduVideoBo bo) {
        EduVideo update = BeanUtil.toBean(bo, EduVideo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EduVideo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除课程视频
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<EduVideo> list(QueryWrapper<EduVideo> wrapperVideo) {
        return baseMapper.selectList(wrapperVideo);
    }
}
